package com.mingqijia.gassafety.job.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.db.entity.Contacts;
import com.mingqijia.gassafety.db.mapper.ContactsMapper;
import com.mingqijia.gassafety.job.service.ContactsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户留存的联系人表 服务实现类
 * </p>
 *
 * @author wanghai
 * @since 2022-02-22
 */
@Service
@Slf4j
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements ContactsService {

    @Autowired
    ContactsMapper contactsMapper;

    @Override
    public List<Contacts> listByIds(List<Integer> ids, String spId) {
        return contactsMapper.highestListByIds( ids,spId );
    }

    @Override
    public List<Map<String, Object>> selectInfo(String consumerId) {
        return contactsMapper.selectInfo(consumerId);
    }


    /**
     * 批量获取客户联系人
     * @param consumerIds
     * @param spId
     * @return
     */
    public Map<Integer,List<Contacts>> getContacts(List<Integer> consumerIds, String spId) {
        log.info("getContacts-params:{},{}", JSONObject.toJSONString(consumerIds),spId);
        LambdaQueryWrapper<Contacts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.or().eq(Contacts::getIsDeleted,0);
        if (!CollectionUtils.isEmpty(consumerIds)) queryWrapper .in(Contacts::getConsumerId,consumerIds);
        List<Contacts> contacts = contactsMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(contacts))  return null;
        return contacts.stream()
                .collect(Collectors.groupingBy(Contacts::getConsumerId, Collectors.toList()));
    }
}
